package practice;

// 编写代码, 基于 Callable 实现 1+2+3+...+1000
// 带有返回值的任务
// 需要中间类FutureTask来进行辅助，最后用get获取结果

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Main1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                // 细节在这里实现逻辑操作就行
                int sum = 0;
                for (int i = 1; i <= 1000; i++) {
                    sum += i;
                }
                return sum;
            }
        };

        // 中间类进行辅助
        FutureTask futureTask = new FutureTask(callable);

        // 创建线程来实现
        Thread t = new Thread(futureTask);
        t.start();

        // 接收
        System.out.println(futureTask.get());
    }
}
